#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@file_name: creating_timeout_for_task_with_wait_for.py
@date: 2025-07-26 17:54
@author: Falling Snow
@desc: 使用 wait_for 为任务创建超时取消
"""
import asyncio

from util import delay


async def main():
    delay_task = asyncio.create_task(delay(2))

    try:
        result = await asyncio.wait_for(delay_task, timeout=1)
        # 这一行是不会被执行的， 因为在await期间， 该task被超时取消了
        print(repr(result))
    except asyncio.exceptions.TimeoutError:
        print("已经超时")
        print(f"delay_task 是否取消了? 状态: {delay_task.cancelled()}")


if __name__ == '__main__':
    asyncio.run(main())
